﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;

namespace Manager.Controls
{
    public partial class ucAccessoryUpdate : System.Web.UI.UserControl
    {
        public bool IsNew
        {
            get
            {
                return Convert.ToBoolean(ViewState["IsNew"]);
            }
            set
            {
                ViewState["IsNew"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        
        private bool CheckInput(string name)
        {
            if (name == "")
            {
                lblMsg.Text = "Bạn chưa nhập Tên phụ kiện";
                txtAccessoryName.Focus();
                return false;
            }
            return true;
        }

        public void Reset_Control()
        {
            txtAccessoryName.Text = "";
            txtDesc.Text = "";
            txtUnit.Text = "";
            txtPrice.Text = "";
            btnSubmit.Text = "Thêm";
        }

        #region Load_Accessory
        public void Load_Accessory(int accessoryID)
        {
            AccessoryInfo accessoryInfo = AccessoryData.SelectByAccessoryID(accessoryID);

            txtAccessoryName.Text = accessoryInfo.AccessoryName;
            txtDesc.Text = accessoryInfo.AccessoryDescription;
            txtUnit.Text = accessoryInfo.AccessoryUnit;
            txtPrice.Text = accessoryInfo.AccessoryPrice.ToString();
            if (accessoryInfo.AccessoryStatus == true)
            {
                rbtActive.Checked = true;
            }
            else
            {
                rbtDeactive.Checked = false;
            }

            Session["sesID"] = accessoryID.ToString();

            btnSubmit.Text = "Cập nhật";
        }
        #endregion

        #region Submit
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string name = txtAccessoryName.Text.Trim();
            string desc = txtDesc.Text.Trim();
            string unit = txtUnit.Text.Trim();
            double price = 0;
            try
            {
                price = Convert.ToDouble(txtPrice.Text.Trim());
            }
            catch (System.Exception)
            {
                price = 0;
            }
            bool status = true;
            if (rbtActive.Checked)
            {
                status = true;
            }
            else
            {
                status = false;
            }

            if (CheckInput(name))
            {
                if (IsNew)
                {
                    AccessoryInfo accessoryInfo = new AccessoryInfo();
                    accessoryInfo.SiteID = Config.Global.Settings.SiteID;
                    accessoryInfo.AccessoryName = name;
                    accessoryInfo.AccessoryDescription = desc;
                    accessoryInfo.AccessoryUnit = unit;
                    accessoryInfo.AccessoryPrice = price;
                    accessoryInfo.AccessoryStatus = status;

                    AccessoryData.Insert(accessoryInfo);
                }
                else
                {
                    int accessoryID = Convert.ToInt32(Session["sesID"]);
                    AccessoryInfo accessoryInfo = AccessoryData.SelectByAccessoryID(accessoryID);

                    accessoryInfo.AccessoryName = name;
                    accessoryInfo.AccessoryDescription = desc;
                    accessoryInfo.AccessoryUnit = unit;
                    accessoryInfo.AccessoryPrice = price;
                    accessoryInfo.AccessoryStatus = status;

                    AccessoryData.Update(accessoryInfo);
                }

                Response.Redirect("AccessoryManager.aspx");
            }
        }
        #endregion
    }
}